package com.googlecode.contests_repo.zip;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.googlecode.contests_repo.annotation.ResourcePrivate;
import com.googlecode.contests_repo.core.model.ReflectionUtils;
import com.googlecode.contests_repo.core.model.Series;
import com.googlecode.contests_repo.core.users.AccessRight;

/**
 * A {@link ResourceZiper} for {@link Series}s. 
 * 
 * @author deni, boyan
 */
public class SeriesZiper extends ResourceZiper {

	@Override
	public void processJsonFile(AccessRight right, String resourcePath,
			String zipPath, ZipOutputStream zip) throws JsonParseException,
			JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		File seriesJsonFile = new File(resourcePath, "series.json");
		Series series = mapper.readValue(seriesJsonFile, Series.class);
		
		if (!right.includes(AccessRight.VIEW_FULL)) {
			ReflectionUtils.setAnnotatedFields(series, ResourcePrivate.class, null);
		}
		
		String path = ("".equals(zipPath) ? "series.json" : zipPath + "/" + "series.json");
		zip.putNextEntry(new ZipEntry(path));
		byte[] bytes = mapper.writeValueAsBytes(series);
		zip.write(bytes);
	}
	
	@Override
	public ResourceZiper getChildZiper() {
		return new ContestZiper();
	}

}
